<?php

namespace app\backend\service;

use app\backend\validate\TaskValidate;
use app\common\model\TaskModel;
use app\common\RootService;

class TaskService extends RootService
{
    /**
     * 标签映射关系
     * @return array
     */
    public function labelData()
    {
        return ['typeLabel' => TaskModel::$typeLabel, 'adminList' => AdminService::adminNames(0, true),
            'stateLabel' => TaskModel::$stateLabel, 'cycleLabel' => TaskModel::$cycleLabel, 'color' => TaskModel::$color];
    }

    /**
     * 任务列表
     * @param array $params
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function lists(array $params)
    {
        // 数据源
        $taskList = $this->page($this->taskModel($params)->order(['sort' => 'asc']));
        // 数据处理
        $taskList['data'] = $this->formatData($taskList['data']);
        return $taskList;
    }

    /**
     * 添加任务
     * @param array $params
     * @return string[]
     * @throws \app\common\RootException
     */
    public function add(array $params)
    {
        // 参数校验
        $this->validate((new TaskValidate()), 'add');
        // 添加失败
        TaskModel::create($this->formData($params)) || $this->errorException('添加失败');
        // 添加成功
        return ['msg' => '添加成功', 'preload' => 'true'];
    }

    /**
     * 编辑任务
     * @param array $params
     * @return string[]
     * @throws \app\common\RootException
     */
    public function edit(array $params)
    {
        // 参数校验
        $this->validate((new TaskValidate()), 'edit');
        // 修改失败
        TaskModel::update($this->formData($params)) || $this->errorException('修改失败');
        // 修改失败
        return ['msg' => '修改成功', 'preload' => 'true'];
    }

    /**
     * 任务详情
     * @param int $taskId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(int $taskId)
    {
        return TaskModel::where(['id' => $taskId])->find()->toArray();
    }

    /**
     * 数据处理
     * @param array $params
     * @return array
     */
    protected function formData(array $params)
    {
        $sqlData = ['cycle' => $params['cycle'], 'zh_title' => $params['zhTitle'], 'title' => $params['title'],
            'zh_subtitle' => $params['zhSubtitle'], 'subtitle' => $params['subtitle'], 'coin' => $params['coin'],
            'diamond' => $params['diamond'], 'remark' => $params['remark'], 'zh_button' => $params['zhButton'],
            'state' => $params['state'], 'quantity' => $params['quantity'], 'button' => $params['button'],
            'link' => $params['link'], 'type' => $params['type'], 'sort' => $params['sort']];
        // 新增 OR 更新
        empty($params['id']) ? $sqlData['admin_id'] = adminInfo('id') : $sqlData['id'] = $params['id'];
        return $sqlData;
    }

    /**
     * 数据处理
     * @param array $taskData
     * @return array
     */
    protected function formatData(array $taskData)
    {
        $labelData = $this->labelData();
        array_walk($taskData, function (&$row) use ($labelData) {
            $row['switchValue'] = $row['state'];
            $row['switchText'] = $labelData['stateLabel'];
            empty($row['link']) && $row['link'] = '--';
            empty($row['remark']) && $row['remark'] = '--';
            $row['color'] = $labelData['color'][$row['state']];
            $row['typeLabel'] = $labelData['typeLabel'][$row['type']] ?? '--';
            $row['stateLabel'] = $labelData['stateLabel'][$row['state']] ?? '--';
            $row['cycleLabel'] = $labelData['cycleLabel'][$row['cycle']] ?? '--';
            $row['adminName'] = $labelData['adminList'][$row['admin_id']] ?? '--';
        });
        return $taskData;
    }

    /**
     * 任务模型
     * @param array $params
     * @return TaskModel
     */
    protected function taskModel(array $params)
    {
        // 标题筛选
        empty($params['zhTitle']) || $where[] = ['zh_title|zh_subtitle', 'like', $params['zhTitle'] . '%'];
        // 类型筛选
        condition($params['type'] ?? '') && $where[] = ['type', 'in', explode(',', $params['type'])];
        // 状态筛选
        condition($params['state'] ?? '') && $where[] = ['state', 'in', explode(',', $params['state'])];
        // 添加人筛选
        condition($params['adminId'] ?? '') && $where[] = ['admin_id', 'in', explode(',', $params['adminId'])];
        // 筛选字段
        $fields = ['*'];
        // 筛选结果
        return TaskModel::where($where ?? [])->field($fields);
    }
}